package cn.tanglong.org.service.impl;

import cn.tanglong.basic.service.impl.BaseServiceImpl;
import cn.tanglong.basic.util.PageList;
import cn.tanglong.org.domain.Department;
import cn.tanglong.org.mapper.DepartmentMapper;
import cn.tanglong.org.query.DepartmentQuery;
import cn.tanglong.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
        update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        if(department.getParent() == null){
            // 没有parent的话 直接把/+自己的id复制给deipath
            department.setDirPath("/" + department.getId());
        }else{
            // 有parent的话 先通过parent获取上级id

            Department pid = departmentMapper.loadById(department.getParent().getId());
            // 然后通过上级id获得上级的dirpath
            String pDirPath = pid.getDirPath();
            // 最后把上级dirpath和自己的id一起赋值给自己的dirpath
            department.setDirPath(pDirPath+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> queryTree() {
        return departmentMapper.queryTree();
    }
}
